import 'package:dio/dio.dart';

class AddressAPI {
  final Dio _dio;

  AddressAPI(this._dio);

  // 获取收货地址列表
  Future<dynamic> list({int page = 1, int limit = 10}) async {
    Response res = await _dio.get('/address/list',
      queryParameters: {
        'page': page,
        'limit': limit
      }
    );

    return res.data;
  }

  // 设置默认收获地址
  Future<dynamic> setDefault(int id) async {
    // 表单数据
    FormData formData = FormData.fromMap({
      "id": id,
    });
    Response res = await _dio.post('/address/default/set', data: formData);

    return res.data;
  }

  // 删除收获地址
  Future<dynamic> del(int id) async {
    // 表单数据
    FormData formData = FormData.fromMap({
      "id": id,
    });
    Response res = await _dio.post('/address/del', data: formData);

    return res.data;
  }

  // 添加收获地址
  // id == 0 添加收获地址
  // id != 0 编辑收获地址
  Future<dynamic> add({
    required String realName,
    required String phone,
    required Map address,
    required String detail,
    int isDefault = 0,
    int id = 0
  }) async {
    // 表单数据
    FormData formData = FormData.fromMap({
      "real_name": realName,
      "phone": phone,
      "address": address,
      "detail": detail,
      "is_default": isDefault,
      "id": id,
    });
    Response res = await _dio.post('/address/edit', data: formData);

    return res.data;
  }

  // 获取省市县列表
  Future<dynamic> cityList() async {
    Response res = await _dio.get('/city_list');

    return res.data;
  }
}